package com.srwl.mytx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.utils.DateUtils;
import com.srwl.mytx.Constant;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.R;
import com.srwl.mytx.domain.Coupon;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.utils.Utils;

import java.util.ArrayList;

/**
 * 一对多红包领取记录适配器
 */
public class MultipleCouponReceiverItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<Coupon> couponList;

    private boolean bol_noData;//标记是否有数据
    private boolean bol_requestError;//标记是否请求出错


    private Context mContext;


    public MultipleCouponReceiverItemAdapter(Context context, ArrayList<Coupon> couponList) {
        mContext = context;
        this.couponList = couponList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            return new NoDataViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.no__data__layout, parent, false));
        } else if (viewType == Constant.ITEM_TYPE_REQUEST_ERROR) {
            return new RequestErrorViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.request_error_layout, parent, false));
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rp_details_list_item, parent, false);

        return new ItemViewHolder(view);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        int viewType = getItemViewType(position);
        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            NoDataViewHolder noDataViewHolder = (NoDataViewHolder) holder;
            noDataViewHolder.tv_tips.setText("没有记录");
        } else if (viewType == Constant.ITEM_TYPE_NORMAL) {
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            setItemViews(itemViewHolder, position);
        }
    }


    private void setItemViews(ItemViewHolder itemViewHolder, int position) {
        Coupon redPacketInfo = couponList.get(position);
        User targetUser = redPacketInfo.getTargetUser();
        if (targetUser != null) {
            itemViewHolder.tvUserName.setText(targetUser.getUsername());
            GlideLoadUtils.glideLoad(mContext, targetUser.getAvatar(), itemViewHolder.ivAvatar, R.drawable.tx_default_avatar_1);
        }
        itemViewHolder.tvMoneyAmount.setText(Utils.moneyFormat(redPacketInfo.getAmount()));
        itemViewHolder.tvDate.setText(DateUtils.getTimestampString(redPacketInfo.getCreated()));

        if (redPacketInfo.getType() == Constant.COUPON_TYPE_MULTIPLE_RANDOM) {
//            if (redPacketInfo.isBest) {
//                itemViewHolder.tvBestLuck.setVisibility(View.VISIBLE);
//            } else {
//                itemViewHolder.tvBestLuck.setVisibility(View.GONE);
//            }
        }
    }

    @Override
    public int getItemCount() {
        if (bol_noData || bol_requestError) {
            return 1;
        }
        return couponList.size();
    }


    @Override
    public int getItemViewType(int position) {
        if (bol_noData) {
            return Constant.ITEM_TYPE_NO_DATA;
        } else if (bol_requestError) {
            return Constant.ITEM_TYPE_REQUEST_ERROR;
        }
        return Constant.ITEM_TYPE_NORMAL;
    }


    /**
     * 显示普通视图
     * 有数据的时候调用
     */
    public void showNormalView() {
        bol_noData = false;
        bol_requestError = false;
        notifyDataSetChanged();
    }

    /**
     * 显示无数据视图
     */
    public void showNoDataView() {
        bol_noData = true;
        notifyDataSetChanged();
    }

    /**
     * 显示请求数据错误视图
     */
    public void showErrorView() {
        bol_requestError = true;
        notifyDataSetChanged();
    }


    private static class HeaderViewHolder extends RecyclerView.ViewHolder {
        TextView tvUserName;
        TextView tvGreeting;
        TextView tvMoneyAmount;
        TextView tvMoneyStatus;
        ImageView ivAvatar;
        TextView tvMoneyUse;
        ImageView ivGroupIcon;

        HeaderViewHolder(View v) {
            super(v);
            initView(v);
        }

        private void initView(View v) {
            tvUserName = v.findViewById(R.id.tv_money_sender);
            tvGreeting = v.findViewById(R.id.tv_greeting);
            tvMoneyStatus = v.findViewById(R.id.tv_money_status);
            tvMoneyAmount = v.findViewById(R.id.tv_money_amount);
            ivAvatar = v.findViewById(R.id.iv_avatar);
            tvMoneyUse = v.findViewById(R.id.tv_money_use);
            ivGroupIcon = v.findViewById(R.id.iv_group_random);
        }
    }

    private static class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView ivAvatar;
        TextView tvUserName;
        TextView tvDate;
        TextView tvMoneyAmount;
        TextView tvBestLuck;

        ItemViewHolder(View v) {
            super(v);
            initView(v);
        }

        private void initView(View v) {
            ivAvatar = v.findViewById(R.id.iv_item_avatar_icon);
            tvUserName = v.findViewById(R.id.tv_money_to_user);
            tvDate = v.findViewById(R.id.tv_time);
            tvMoneyAmount = v.findViewById(R.id.tv_item_money_amount);
            tvBestLuck = v.findViewById(R.id.tv_best_icon);
        }
    }


}
